package com.tanger.common.utils;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * @Description 转拼音工具类
 * @Author Hchenbin
 * @Date 2022/6/22 10:24
 * @Version 1.0
 */
public class ShortPinyin {

    public static String getShortPinyin(String name) {
        if (StringUtils.hasText(name)) {
            try {
                return PinyinHelper.getShortPinyin(name);
            } catch (PinyinException e) {
                System.err.println("[convert] ShortPinyin exception."+e);
            }
        }
        return null;
    }

    private static final ThreadLocal<String> TEST_DEFAULT_VALUE = new ThreadLocal<String>(){
        @Override
        protected String initialValue() {
            return "测试本地线程";
        }
    };

    public static void main(String[] args) throws InterruptedException {
        for (int i = 1; i < 5; i++) {
            int finalI = i;
            Thread t1 = new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " begin");
                System.out.println("STRING_THREAD_LOCAL = " + TEST_DEFAULT_VALUE.get());
                System.out.println("t1 change");
                TEST_DEFAULT_VALUE.set("我是 t"+ finalI);
                System.out.println("STRING_THREAD_LOCAL = " + TEST_DEFAULT_VALUE.get());
                System.out.println(Thread.currentThread().getName() + " end");
            }, "THREAD:"+i);
            t1.start();

            TimeUnit.SECONDS.sleep(1);
            System.out.println("-----------------------------------------------------------");
        }
    }
}
